package com.ease.util {
   import flash.utils.Dictionary;

   public class WeakReference {
      /**
       * The weak reference <code>Dictionary</code> which stores the reference
       * to the target object.
       *
       * @private
       */
      private var _dictionary:Dictionary = new Dictionary(true);

      /**
       * Constructs a new <code>WeakReference</code> instance.
       *
       * @param object The <code>Object</code> to reference.
       */
      public function WeakReference(object:* = undefined) {
         super();
         this.object = object;
      }

      /**
       * Clears the reference to the target object. After calling this
       * function the <code>object</code> property will return
       * <code>undefined</code>.
       */
      public function clear():void {
         object = undefined;
      }

      /**
       * Indicates whether the target of this <code>WeakReference</code> has
       * been garbage collected or this <code>WeakReference</code> has been
       * cleared.
       */
      public function get cleared():Boolean {
         return object == undefined;
      }

      /**
       * The object to which this <code>WeakReference</code> refers. If the
       * target has been garbage collected or this <code>WeakReference</code>
       * has been cleared, this will be <code>undefined</code>.
       */
      public function get object():* {
         for (var value:Object in _dictionary) {
            return value;
         }
         return undefined;
      }

      /**
       * @private
       */
      public function set object(value:*):void {
         if (object !== undefined) {
            delete _dictionary[object];
         }
         if (value !== undefined) {
            _dictionary[value] = this;
         }
      }
   }
}
